﻿using System;
using System.Linq;
using System.Collections.Generic;
using Domain.Core;
using Domain.MainModule.Entities;
using Domain.MainModule.GroupBranchCurrencies;

namespace Application.MainModule.GroupBranchCurrencyManagement
{
    public class GroupBranchCurrencyManagementService : IGroupBranchCurrencyManagementService, IDisposable
    {
        #region private properties

        private IGroupBranchCurrencyRepository GroupBranchCurrencyRepository
        {
            get;
            set;
        }

        #endregion

        #region constructor

        /// <summary>
        /// Default constructor for GroupBranchService.
        /// </summary>
        /// <param name="groupBranchRepository">GroupBranchRepository dependency, usually this is resolved by IoC</param>
        public GroupBranchCurrencyManagementService(IGroupBranchCurrencyRepository groupBranchRepository)
        {
            if (groupBranchRepository == (IGroupBranchCurrencyRepository)null)
            {
                throw new ArgumentNullException(Resources.Messages.exception_DependenciesAreNotInitialized);
            }

            this.GroupBranchCurrencyRepository = groupBranchRepository;
        }

        #endregion

        #region IGroupBranchService implementations

        public void AddGroupBranchCurrency(GroupBranchCurrency groupBranch)
        {
            IUnitOfWork unitOfWork = this.GroupBranchCurrencyRepository.UnitOfWork as IUnitOfWork;
            this.GroupBranchCurrencyRepository.Add(groupBranch);
            unitOfWork.Commit();
        }

        public void ChangeGroupBranchCurrency(GroupBranchCurrency groupBranch)
        {
            IUnitOfWork unitOfWork = this.GroupBranchCurrencyRepository.UnitOfWork as IUnitOfWork;
            this.GroupBranchCurrencyRepository.Modify(groupBranch);
            unitOfWork.CommitAndRefreshChanges();
        }

        public GroupBranchCurrency FindGroupBranchCurrencyById(Guid groupBranchId)
        {
            GroupBranchCurrencyIdSpecification groupBranchCurrencyIdSpecification = new GroupBranchCurrencyIdSpecification(groupBranchId);
            return this.GroupBranchCurrencyRepository.FindGroupBranchCurrency(groupBranchCurrencyIdSpecification);
        }

        public List<GroupBranchCurrency> FindGroupBranchCurrencyByCurrencyId(Guid currencyId)
        {
            GroupBranchCurrencyCurrencyIdSpecification groupBranchCurrencyIdSpecification = new GroupBranchCurrencyCurrencyIdSpecification(currencyId);
            return this.GroupBranchCurrencyRepository.FindGroupBranchCurrencies(groupBranchCurrencyIdSpecification).ToList<GroupBranchCurrency>();
        }

        public void RemoveGroupBranchCurrency(GroupBranchCurrency groupBranchCurrency)
        {
            if (groupBranchCurrency == (GroupBranchCurrency)null)
                throw new ArgumentNullException("groupBranchCurrency");

            IUnitOfWork unitOfWork = this.GroupBranchCurrencyRepository.UnitOfWork as IUnitOfWork;
            this.GroupBranchCurrencyRepository.Remove(groupBranchCurrency);
            unitOfWork.CommitAndRefreshChanges();

        }

        #endregion

        #region IDisposable implementations

        /// <summary>
        /// Dispose associated context with this domain service.
        /// </summary>
        public void Dispose()
        {
            if (this.GroupBranchCurrencyRepository != null
                &&
                this.GroupBranchCurrencyRepository.UnitOfWork != null)
            {
                this.GroupBranchCurrencyRepository.UnitOfWork.Dispose();
            }
        }

        #endregion
    }
}
